﻿using ExtAspNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Constant;
using FJASPX.Framwork.Model;

namespace FJASPX.Framwork.UI
{
    public static class Ext
    {
        #region 设置控件值

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this Label control, string val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this TextBox control, string val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this NumberBox control, object val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this TextArea control, string val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this DatePicker control, object val)
        {
            if (!control.IsNull())
            {
                if (val is string)
                {
                    control.Text = val.IfNull().Trim();
                    return;
                }
                if (val is DateTime)
                {
                    DateTime dt = val.ToDateTime();
                    if (!dt.Equals(Objects.EMPTY_DATE_TIME))
                    {
                        control.SelectedDate = dt;
                    }
                    return;
                }
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">值</param>
        public static void SetValue(this CheckBox control, object val)
        {
            if (control.IsNull())
            {
                return;
            }
            if (val is bool)
            {
                control.Checked = (bool)val;
                return;
            }
            if (val.ToString().Equals(Strings.NUM_1))
            {
                control.Checked = true;
                return;
            }
            control.Checked = false;
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">值</param>
        public static void SetValue(this DropDownList control, string val)
        {
            if (!control.IsNull())
            {
                control.SelectedValue = val;
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this ToolbarText control, string val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetValue(this TwinTriggerBox control, string val)
        {
            if (!control.IsNull())
            {
                control.Text = val.IfNull().Trim();
            }
        }

        /// <summary>
        /// 设置点击
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static void SetClick(this Button control, string val)
        {
            if (!control.IsNull())
            {
                control.OnClientClick = val.IfNull().Trim();
            }
        }

        public static void SetValue(this HtmlEditor contorl, string val)
        {
            if (!contorl.IsNull())
            {
                contorl.Text = val.IfNull();
            }
        }
        /// <summary>
        /// 绑定控件数据
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">数据</param>
        public static void SetDataSource(this DropDownList control, object data)
        {
            if (!control.IsNull())
            {
                control.DataValueField = Column.Id;
                control.DataTextField = Column.Name;
                control.DataSimulateTreeLevelField = Column.Sort;
                control.DataSource = data;
                control.DataBind();
                control.Items.Insert(0, new ListItem("--请选择--", "0"));
                control.SetValue(StringHelper.GetStrSession(control.ID));
            }
        }
        #endregion

        #region 提取控件值

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static string GetValue(this Label control)
        {
            if (!control.IsNull())
            {
                return control.Text.IfNull().Trim();
            }
            return String.Empty;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static string GetValue(this TextBox control)
        {
            if (!control.IsNull())
            {
                return control.Text.IfNull().FilterHTML().Trim();
            }
            return String.Empty;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static int GetValue(this NumberBox control)
        {
            if (!control.IsNull())
            {
                return control.Text.ToInt();
            }
            return 0;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static decimal GetDecValue(this NumberBox control)
        {
            if (!control.IsNull())
            {
                return control.Text.ToDec();
            }
            return 0;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static string GetValue(this TextArea control)
        {
            if (!control.IsNull())
            {
                return control.Text.IfNull().FilterHTML().Trim();
            }
            return String.Empty;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static DateTime GetValue(this DatePicker control)
        {
            if (!control.IsNull())
            {
                return control.SelectedDate.ToDateTime();
            }
            return Objects.EMPTY_DATE_TIME;
        }

        /// <summary>
        /// 提取控件值为空时返回最小
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static DateTime GetValueEmptyMin(this DatePicker control)
        {
            if (!control.IsNull() && !control.Text.IsEmpty())
            {
                return control.SelectedDate.ToDateTime();
            }
            return Objects.EMPTY_DATE_TIME;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="val">值</param>
        public static int GetValue(this CheckBox control)
        {
            if (control.IsNull())
            {
                return 0;
            }
            return control.Checked ? 1 : 0;
        }

        /// <summary>
        /// 提取控件值
        /// </summary>
        /// <param name="control">控件</param>
        public static string GetValue(this DropDownList control)
        {
            if (control.IsNull())
            {
                return String.Empty;
            }
            StringHelper.SetSession(control.ID, control.SelectedValue);
            return control.SelectedValue;
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        public static string GetValue(this CheckBoxList control)
        {
            string ret = String.Empty;
            if (control.IsNull())
            {
                return ret;
            }
            for (int i = 0; i < control.Items.Count; i++)
            {
                if (control.Items[i].Selected)
                {
                    ret += Strings.COMMA + control.Items[i].Value.Trim();
                }
            }
            if (ret.Length > 0)
            {
                ret = ret.Substring(1);
            }
            return ret;
        }

        /// <summary>
        /// 设置控件值
        /// </summary>
        /// <param name="control">控件</param>
        public static List<int> GetIntValue(this CheckBoxList control)
        {
            List<int> ret = new List<int>();
            if (control.IsNull())
            {
                return ret;
            }
            for (int i = 0; i < control.Items.Count; i++)
            {
                if (control.Items[i].Selected)
                {
                    ret.Add(control.Items[i].Value.Trim().ToInt());
                }
            }
            return ret;
        }

        /// <summary>
        /// 提取ICON图标地址
        /// </summary>
        /// <param name="icon">图标</param>
        /// <returns>图标地址</returns>
        public static string GetIconUrl(this Icon icon)
        {
            if (!icon.IsNull())
            {
                return IconHelper.GetIconUrl(icon);
            }
            return string.Empty;
        }
        /// <summary>
        /// 提取HtmlEditor值
        /// </summary>
        /// <param name="htmlEditor"></param>
        /// <returns></returns>
        public static string GetValue(this HtmlEditor htmlEditor)
        {

            if (!htmlEditor.IsNull())
            {
                return htmlEditor.Text.IfNull();
            }
            return String.Empty;
        }
        #endregion
    }
}
